﻿#include <iostream>
using namespace std;

//两个数组的交集1
//https://leetcode-cn.com/problems/intersection-of-two-arrays/
class Solution {
public:


    vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {
        unordered_set<int> s1(nums1.begin(),nums1.end());
        unordered_set<int> s2(nums2.begin(),nums2.end());


        vector<int> v;
        auto it = s2.begin();
        while(it != s2.end())
        {
            if(s1.find(*it) != s1.end())
                v.push_back(*it);
            
            ++it;
        }
        return v;
    }
};





//两句话中的不常见单词
//https://leetcode-cn.com/problems/uncommon-words-from-two-sentences/
class Solution {
public:
    vector<string> uncommonFromSentences(string s1, string s2) {
        vector<string> ret;
        vector<string> tmp;
        string str;
        s1.push_back(' ');
        s1 += s2;
         for(const auto& s:s1)
         {
             if(s == ' ')
             {
                 tmp.push_back(str);
                 str.clear();
                 continue;
             }
             str.push_back(s);
         }
        tmp.push_back(str);
        unordered_map<string,int> m;
        for(const auto& x:tmp)
        {
            ++m[x];
        }
        for(const auto& x:m)
        {
            if(x.second == 1)
                ret.push_back(x.first);
        }
        return ret;
    }
};



//两个数组的交集2
//https://leetcode-cn.com/problems/intersection-of-two-arrays-ii/
class Solution {
public:
    vector<int> intersect(vector<int>& nums1, vector<int>& nums2) {
        unordered_map<int,int> m;
        for(const auto& x:nums1)
        {
            ++m[x];
        }
        vector<int> ret;
        for(const auto& x:nums2)
        {
            if(m.find(x) != m.end() && m[x] != 0)
            {
                --m[x];
                ret.push_back(x);
            }
        }
        return ret;
    }
};
